package com.woniuxy.rbac.web;

import com.woniuxy.rbac.entity.Course;
import com.woniuxy.rbac.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class IndexController {

    @Autowired
    CourseService courseService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public String index(Model model){
        System.out.println(Thread.currentThread().getName());

        List<Course> courses = (List<Course>) redisTemplate.opsForValue().get("courses");

        if (courses ==null) {
            courses = courseService.list();
            redisTemplate.opsForValue().set("course",courses);
        }

        model.addAttribute("course",courses);
        model.addAttribute("courses", courseService.list());

        return "index";
    }
}
